﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

using OneSystems.Data.Entities;

namespace OneSystems.BusinessLogic
{
    public sealed class DebtLogic
    {
        private DebtLogic() { }

        #region public properties
        public static IList<Category> Categories
        {
            get
            {
                return Category.GetAll();
            }
        }
        public static IList<Creditor> Creditors
        {
            get
            {
                return Creditor.GetAll();
            }
        }
        public static IList<Brand> Brands
        {
            get
            {
                return Brand.GetAll();
            }
        }
        #endregion

        #region Get

        public static IList<Account> GetDebt(int categoryID)
        {
            IQueryable<Account> match = Account.GetQueryable();
            match = match.Where(c => c.CategoryID == categoryID);
            return match.ToList();
        }


        public static IList<Account> GetAccountsByCategory(int categoryID, int debtorID)
        {
            IQueryable<Account> match = Account.GetQueryable();
            match = match.Where(c => c.CategoryID == categoryID && !c.ClosedDate.HasValue || c.ClosedDate.Value > DateTime.Today.AddDays(-1));
            match = match.Where(c => c.DebtorID == debtorID);
            return match.ToList();
        }
        public static Account GetAccount(int accountID)
        {
            return Account.GetOne(accountID);
        }
        public static Category GetCategory(int categoryID)
        {
            return OneSystems.Data.Entities.Category.GetOne(categoryID);
        }

        #endregion

        #region Save
        public static Brand SaveBrand(int brandID, int categoryID, string name, string description, System.Drawing.Image logoImage, string fileExtension)
        {
            Brand brand = new Brand();
            if (brandID != 0)
            {
                brand = OneSystems.Data.Entities.Brand.GetOne(brandID);
            }
            brand.CategoryID = categoryID;
            brand.Name = name;
            brand.Description = description;
            brand.LogoImage = OneSystems.Helpers.ImageHelper.ConvertImageToByteArray(logoImage);
            brand.Save();
            
            return brand;
        }
        public static Category SaveCategory(int categoryID, string name, string desciption, System.Drawing.Image logoImage)
        {
            Category category = new Category();
            if (categoryID != 0)
            {
                category = OneSystems.Data.Entities.Category.GetOne(categoryID);
            }
            category.Name = name;
            category.Description = desciption;
            category.LogoImage = OneSystems.Helpers.ImageHelper.ConvertImageToByteArray(logoImage);
            category.Save();
            return category;
        }
        /// <summary>
        /// Save a new or existing account to the database.
        /// </summary>
        /// <param name="accountID">Id of a prior account or 0 if it's brand new.</param>
        /// <param name="creditOfferingID">Credit Offering Primary key.</param>
        /// <param name="debtorID">Id of the person making the account.</param>
        /// <param name="openDate">Date the account is opened</param>
        /// <param name="closedDate">Date the account is closed.</param>
        /// <param name="accountNumber">The account number.</param>
        /// <returns>Returns the newly saved account.</returns>
        public static Account SaveAccount(int accountID, int creditorID, int debtorID, 
            int categoryID, int? brandID, DateTime openDate, DateTime? closedDate, 
            string accountNumber, decimal interestRateValue, decimal cashAdvanceRate, decimal creditLimitAmount, 
            string accountNickname, decimal minimumPayment, decimal paymentPercentage, DateTime dueDay)
        {
            Account account = new Account();
            if (accountID != 0)
            {
                account = Account.GetOne(accountID);
            }
            account.CreditorID = creditorID;
            account.DebtorID = debtorID;
            account.CategoryID = categoryID;
            account.BrandID = brandID;
            account.OpenDate = openDate;
            account.ClosedDate = closedDate.Value;
            account.AccountNumber = accountNumber;
            account.Nickname = accountNickname;
            account.MinimumPayment = minimumPayment;
            account.PaymentPercentage = paymentPercentage;
            int DueDay = int.Parse(dueDay.ToString("dd"));
            account.DueDay = DueDay;

            try
            {
                account.Save();
                account.CreditLimit = creditLimitAmount;
                account.InterestRate = interestRateValue;
                account.CashAdvanceRate = cashAdvanceRate;


                return account;
            }
            catch (Exception ex)
            {
                throw new Exception("The account could not be saved.", ex);
            }
        }

        #endregion
    }
}
